package com.bawei.day0129.utils.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bawei.day0129.Bean.JkzxBean;
import com.bawei.day0129.R;
import com.bawei.day0129.view.activity.XqActivity;
import com.bumptech.glide.Glide;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author 11295 刘铠源
 * 2023/1/30 15:36
 * @类名 RecAdapter
 */
public class RecAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private ArrayList<JkzxBean.ResultBean> list=new ArrayList<>();
    private Context context;

    public RecAdapter(ArrayList<JkzxBean.ResultBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType==1){
            View inflate = View.inflate(context, R.layout.layoutjk1, null);
            return new Holder1(inflate);
        }else {
            View inflate = View.inflate(context, R.layout.layoutjk2, null);
            return new ViewHolder2(inflate);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof Holder1){
            final Holder1 h1 = (Holder1) holder;
            Glide.with(context)
                    .load(list.get(position).getThumbnail())
                    .into((ImageView)h1.img);
            h1.name1.setText(list.get(position).getTitle());
            h1.name2.setText(list.get(position).getSource());
            final long releaseTime = list.get(position).getReleaseTime();
            final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            final String format = simpleDateFormat.format(new Date(releaseTime));
            h1.name3.setText(format);
            h1.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final Intent intent = new Intent(context, XqActivity.class);
                    intent.putExtra("id",list.get(position).getId());
                    context.startActivity(intent);
                }
            });
        }else {
            final ViewHolder2 h1 = (ViewHolder2) holder;
            final String[] split = list.get(position).getThumbnail().split(";");
            Glide.with(context)
                    .load(split[0])
                    .into((ImageView)h1.img1);
            Glide.with(context)
                    .load(split[1])
                    .into((ImageView)h1.img2);
            Glide.with(context)
                    .load(split[2])
                    .into((ImageView)h1.img3);
            h1.name1.setText(list.get(position).getTitle());
            h1.name2.setText(list.get(position).getSource());
            h1.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final Intent intent = new Intent(context, XqActivity.class);
                    intent.putExtra("id",list.get(position).getId());
                    context.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        String[] split = list.get(position).getThumbnail().split(";");
        if (split.length==1){
            return 1;
        }else {
            return 2;
        }
    }

    class Holder1 extends RecyclerView.ViewHolder{
        private ImageView img;
        private TextView name1,name2,name3;
        public Holder1(@NonNull View itemView) {
            super(itemView);
            img=itemView.findViewById(R.id.img1);
            name1=itemView.findViewById(R.id.name1);
            name2=itemView.findViewById(R.id.name11);
            name3=itemView.findViewById(R.id.name111);
        }
    }
    class ViewHolder2 extends RecyclerView.ViewHolder {
        private ImageView img1,img2,img3;
        private TextView name1,name2;
        public ViewHolder2(@NonNull View itemView) {
            super(itemView);
            img1=itemView.findViewById(R.id.img2);
            img2=itemView.findViewById(R.id.img21);
            img3=itemView.findViewById(R.id.img22);
            name1=itemView.findViewById(R.id.name2);
            name2=itemView.findViewById(R.id.name21);
        }
    }
}
